﻿using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using PS.Utils.FPRPC;

namespace PS.WssSync.Tests
{
    [TestFixture]
    public class FrontPageRPCTests
    {
        private FrontPageRPC frontPageService;

        [TestFixtureSetUp]
        public void Init()
        {            
            frontPageService = new FrontPageRPC();
        }

        [Test]
        public void GetServerVersion()
        {
            WebUrl result = frontPageService.UrlToWebUrl("http://test01/");
            string version = frontPageService.GetServerExtensionsVersion(result);
            Assert.AreEqual(version, "12.0.0.4518");
        }

        [Test]
        public void UrlToWebUrl()
        {
            WebUrl result = frontPageService.UrlToWebUrl("http://test01/loc-test/dasdasdas");

            Assert.AreEqual(result.SiteUrl, "http://test01/loc-test");
        }

        [Test]
        public void GetDocument()
        {
            using (FileStream s = new FileStream(@"D:\tmp\out.master", FileMode.Create))
            {
                WebUrl url = frontPageService.UrlToWebUrl("http://test01/loc-test/_catalogs/masterpage/default.master");
                DocumentInfo d = frontPageService.GetDocument(url, s);
                Assert.IsNotNull(d);
            }
        }

        [Test]
        [ExpectedException(ExceptionType = typeof(FrontPageRPCException))]
        public void GetDocumentFail()
        {
            using (FileStream s = new FileStream(@"D:\tmp\out.master", FileMode.Create))
            {
                WebUrl url = frontPageService.UrlToWebUrl("http://test01/loc-test/_catalogs/masterpage/give.me.error");
                DocumentInfo d = frontPageService.GetDocument(url, s);
                Assert.IsNotNull(d);
            }
        }

        [Test]
        public void GetDocumentMetaInfo()
        {
            WebUrl url = frontPageService.UrlToWebUrl("http://test01/loc-test/_catalogs/masterpage/default.master");
            DocumentInfo i = frontPageService.GetDocumentMetaInfo(url);
            Assert.IsTrue(!i.IsFolder);
            Assert.IsTrue(i.Exists);
        }

        [Test]
        public void GetDocumentMetaInfoDir()
        {
            WebUrl url = frontPageService.UrlToWebUrl("http://test01/loc-test/_catalogs/masterpage");
            DocumentInfo i = frontPageService.GetDocumentMetaInfo(url);
            Assert.IsTrue(i.IsFolder);
            Assert.IsTrue(i.Exists);
        }

        [Test]
        public void GetDocumentMetaInfoFail()
        {
            WebUrl url = frontPageService.UrlToWebUrl("http://test01/loc-test/_catalogs/masterpage/give.me.error");
            DocumentInfo i = frontPageService.GetDocumentMetaInfo(url);
            Assert.IsTrue(!i.Exists);
        }

        [Test]
        public void GetDocumentMetaInfoRus()
        {
            WebUrl url = frontPageService.UrlToWebUrl("http://test01/loc-test/тест.txt");
            DocumentInfo i = frontPageService.GetDocumentMetaInfo(url);
            Assert.IsTrue(i.Exists);
        }

        [Test]
        public void ListDirectories()
        {
            WebUrl webUrl = frontPageService.UrlToWebUrl("http://test01/loc-test/");
            List<WebUrl> dirs = frontPageService.ListFolders(webUrl);
            Assert.IsTrue(dirs.Count > 0);
        }

        [Test]
        [ExpectedException(ExceptionType = typeof(FrontPageRPCException))]
        public void ListDirectoriesFail()
        {
            WebUrl webUrl = frontPageService.UrlToWebUrl("http://test01/loc-test/_give_me_error");
            frontPageService.ListFolders(webUrl);
        }

        [Test]
        public void ListFiles()
        {
            WebUrl webUrl = frontPageService.UrlToWebUrl("http://test01/loc-test/");
            List<WebUrl> files = frontPageService.ListDocuments(webUrl, false);
            Assert.IsTrue(files.Count > 0);
        }

        [Test]
        [ExpectedException(ExceptionType = typeof(FrontPageRPCException))]
        public void ListFilesFail()
        {
            WebUrl webUrl = frontPageService.UrlToWebUrl("http://test01/loc-test/_gime_me_error");
            frontPageService.ListDocuments(webUrl, true);
        }

        [Test]
        public void CheckInOutDocument()
        {
            WebUrl url = frontPageService.UrlToWebUrl("http://test01/loc-test/_catalogs/masterpage/default.master");
            frontPageService.CheckOutDocument(url);
            frontPageService.CheckInDocument(url, string.Empty);
        }

        [Test]
        public void CheckOutAndUndoDocument()
        {
            WebUrl url = frontPageService.UrlToWebUrl("http://test01/loc-test/_catalogs/masterpage/default.master");
            frontPageService.CheckOutDocument(url);
            frontPageService.UnCheckOutDocument(url);
        }

        [Test]
        public void CreateDirectory()
        {
            WebUrl url = frontPageService.UrlToWebUrl("http://test01/loc-test/_newfolder");
            frontPageService.CreateDirectory(url);
        }

        [Test]
        public void PutDocument()
        {
            WebUrl url = frontPageService.UrlToWebUrl("http://test01/loc-test/_style library/add.txt");
            using (FileStream s = new FileStream(@"D:\tmp\тест.txt", FileMode.Open))
            {
                frontPageService.PutDocument(url, s);
            }
        }

        [Test]
        public void GetAllWebs()
        {
            WebUrl url = frontPageService.UrlToWebUrl("http://Saber-test:81/");
            string s = frontPageService.ListWebs(url);
        }
    }
}